//引入接口
import API from '../../config/server.js'
// const API=require('../../config/server.js')
Page({
  data:{
    nav:[],
		heroList:[],
    allList:[],
		navType:'all'
  },
  onLoad()
  {
    //生命周期钩子
		this.heroData()
  },
	
	heroData(){
		let that=this
		wx.showLoading({
			title: '加载中'
		})
		wx.request({
			url: API.herolist,
			method: 'GET',
			dataType: 'json',
			success: function (res) { 
				let data = res.data.data
				that.setData({
					nav: data.nav,
					heroList: data.heroList,
          allList:data.heroList
				})
			},
			fail: function (res) {
				wx.showToast({
					title: '网络错误',
					icon: 'error',
				})
			 },
			complete: function (res) {
				wx.hideLoading()
			 },
		})
	},
	changeNav(e){
		let type = e.currentTarget.dataset.type
    let newList=[]
    let list=this.data.allList
    if(type=='all'){
      newList=list
    }else{
      list.filter(function (hero) {
        if (hero.camptype == type) {
          newList.push(hero)
        }
      })
    }
		this.setData({
			navType: type,
      heroList: newList
		})	
	},
	toDetail(e){
    let id = e.currentTarget.dataset.id
    let url=e.currentTarget.dataset.url
		wx.navigateTo({
			url: `/pages/heroDetail/heroDetail?id=${id}&url=${url}`,
			success: function(res) {},
			fail: function(res) {},
			complete: function(res) {},
		})
	}
})